package client.comm.baoding.ui

import android.content.Intent
import androidx.lifecycle.Observer
import client.comm.commlib.toast.toast
import client.comm.baoding.R
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.ActivityBanktxBinding
import client.comm.baoding.dialog.PayDetailDialog
import client.comm.baoding.ui.vm.TxViewModel
import client.comm.commlib.dialog.ValidatePwdDialog
import client.comm.commlib.utils.StoreUtil
import com.blankj.utilcode.util.LogUtils

class BankTxActivity :
    BaseMiddleActivity<TxViewModel, ActivityBanktxBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_banktx
    }

    override fun initViewModel(): TxViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel

        StoreUtil.getBean<UserInfo>(this@BankTxActivity)?.let {
            viewModel.userInfo.value = it
            binding.incomeTx= it.income_tx.toString()
        }
    }

    override fun initData() {
    }

    override fun observerUI() {
        viewModel.call_kt.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    it.data?.let {
                        startActivity(Intent(this@BankTxActivity, Bank3CodeActivity::class.java).apply {
                            putExtra("bank_bind", it)
                            putExtra("status", "3")
                        })
                    }
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })
    }

    fun ok() {
        if (viewModel.txNum.value == null || viewModel.pType.value!! < 0) {
            toast("所有不得为空")
            return
        }
        if (viewModel.pType.value == 3) {
            PayDetailDialog(this,"立即提现").apply {
                vm = viewModel
                payMoney.value = "${viewModel.txNum.value}"

                onSelectBank = { money: String, bankNo: String, linkId: String ->
                    LogUtils.i("${money + " - " + bankNo + " - " + linkId}")
                    ValidatePwdDialog(this@BankTxActivity).apply {
                        title = "需要验证支付密码"
                        ok = {
                            viewModel.ok_tx(it,linkId!!)
                        }

                    }.show()
                }
            }.show()
        }
    }

    fun goRecord() {
        startActivity(Intent(this, KtOrderActivity::class.java)) //RechargeRecordActivity
    }
}